/* -*- c++ -*- */
#ifndef __MyMotionState_h_
#define __MyMotionState_h_

#include <btBulletDynamicsCommon.h>
#include "Object.h"

//Here is defined a MotionState. The MotionState is the way used by Bullet
//to join the physic body with the graphic object, so changes made at the
//physic body are translated to changes made at the graphic object. 
class MyMotionState : public btMotionState {
	
	public:
		MyMotionState(const btTransform &initialTransform, Object* obj) {
			_visibleObject = obj;
			_transform = initialTransform;
		}

		virtual ~MyMotionState() {}

		void setVisibleObject(Object* obj) {
			_visibleObject = obj;
		}

		virtual void getWorldTransform(btTransform &worldTrans) const {
			worldTrans = _transform;
		}

		virtual void setWorldTransform(const btTransform &worldTrans) {
			std::cout << "SETTING WORLD TRANSFORM" << std::endl;
			if(NULL == _visibleObject) return; // silently return before we set a node
			btQuaternion rot = worldTrans.getRotation();
			//Set the orientation of the visible object according to the rotation of the physic body
			//mVisibleobj->setOrientation(rot.w(), rot.x(), rot.y(), rot.z());
			
			//Set the position of the visible object according to the position of the physic body
			btVector3 pos = worldTrans.getOrigin();
			//_visibleObject->set_centre(pos.x(), pos.y(), pos.z()); 
			_transform = worldTrans;
		}

	protected:
		Object* _visibleObject;
		btTransform _transform;
};

#endif 